<?php
class WebsiteController extends Controller {
	
	public function indexAction() {
        $this->checkLoginRedirect();
        $this->setErrors();
        $user = $this->_session->getCurrent();
		$websites = $this->_model->getAllWebsites($user->id);
		$commonAccessWebsites = $this->_model->getCommonAccessWebsites($user->id);
		$websites = array_merge($websites, $commonAccessWebsites);
		$this->_view->assignArray(array(
			'pageTitle' => 'Localization system :: Leksem List',
			'pageId' => 'website',
			'websites' => $websites
		));
		$this->_view->render('website/index');
	}
	
	public function addnewAction() {
        $this->checkLoginRedirect();
		$websiteName = $this->_request->getString('wsname');
		$websiteUrl = $this->_request->getString('wsurl');
		
		if(empty($websiteName)) {
			$this->addError("Website name can not be empty");
		}
		if(empty($websiteUrl)) {
			$this->addError("Website url can not be empty");
		}
		
		$user = $this->_session->getCurrent();
		
		if(!$this->isErrors()) {
			$website = new Website();
			$website->name = $websiteName;
			$website->url = $websiteUrl;
			$website->userId = $user->id;
			$website->save();
            $this->redirect('website');
		}
        else {
            $this->delegateToAction('index');
        }
	}

    public function listAction() {
        $this->checkLoginRedirect();

        $user = $this->_session->getCurrent();
        $websites = $this->_model->getAllWebsites($user->id);

        $this->_view->assignArray(array(
			'pageTitle' => 'Localization system :: Websites list',
			'pageId' => 'websitelist',
			'websites' => $websites
		));
		$this->_view->render('website/list');
    }

    public function userslistAction() {
        $this->checkLoginRedirect();
        $wsId = $this->_request->getInt('wsId');
        $usersList = new Userslist();

        $this->_view->assignArray(array(
			'pageTitle' => 'Localization system :: Users list',
			'pageId' => 'wsuserslist',
            'wsId' => $wsId,
            'users' => $usersList->getList()
		));
        $this->_view->render('website/commonaccess');
    }

    public function runaddUserAction() {
        $preparedUsers = array();
        $checkedUsers = $this->_request->getArray('checkedUsers');
        $websiteId = $this->_request->getInt('hidWsId');
        $access = $this->_request->getArray('access');
        foreach ($checkedUsers as $uId) {
            $userAccess = $access[$uId];
            $this->_model->saveCommonAccess($websiteId, $uId, $userAccess);
        }
    }
}